package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

import java.util.ArrayList;
import java.util.Map;


@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    //根据用户的id查询喜欢的人的id
    @Override
    public List<Long> findUserLike(Long userId, boolean flag) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(flag));
        //查询
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //获取其中喜欢人的id
        return CollUtil.getFieldValues(userLikes,"likeUserId",Long.class);
    }

    //根据用户的id查询喜欢自己的用户id
    @Override
    public List<Long> findFans(Long userId, boolean flag) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(flag));
        //查询
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //获取其中喜欢人的id
        return CollUtil.getFieldValues(userLikes,"userId",Long.class);
    }
    /*
    是否喜欢
     */
    @Override
    public UserLike whetherLike(Long userId, Long likeUserId) {

        Criteria criteria = Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId)
                .and("isLike").is(true);
        Query query = Query.query(criteria);

        UserLike like = mongoTemplate.findOne(query, UserLike.class);
        return like;

    }

    /*
    互相喜欢分页
     */
    @Override
    public List<UserLike> otherLike(Long userId) {

        //当前用户喜欢的
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        List<UserLike> userLike = mongoTemplate.find(query, UserLike.class);
        if (userLike == null) {
            return new ArrayList<>();
        }
        //喜欢用户也喜欢当前 互相喜欢
        List<UserLike> otherLike = new ArrayList<>();

        for (UserLike like : userLike) {
            Query query1 = Query.query(Criteria.where("userId").is(like.getLikeUserId())
                    .and("isLike").is(true));
            UserLike one = mongoTemplate.findOne(query1, UserLike.class);
            if (one != null) {
                otherLike.add(one);
            }

        }
            return otherLike;
    }

    @Override
    public List<UserLike> findById(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true)).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    /*
    喜欢

     */
    @Override
    public List<UserLike> like(Long userId) {
        //放入返回数据
        //查询我喜欢
        Query query = Query.query(Criteria.where("userId").is(userId)
                        .and("isLike").is(true));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        //判断不为空存入
        if (list!=null) {
           return list;
        }
        return new ArrayList<>();
    }

    /*
    粉丝
     */
    @Override
    public List<UserLike> likeMe(Long userId) {
        //放入返回数据
        //查询我喜欢
        Query query = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        //判断不为空存入
        if (list!=null) {
            //符合条件存入
            return list;
        }
        return new ArrayList<>();
    }

    /*
    粉丝喜欢
     */
    @Override
    public void fansLike(Long userId, Long fansUserId) {
        //粉丝条件
        //查询我喜欢
        //符合条件存入
              UserLike userLike = new UserLike();
                userLike.setUserId(fansUserId);
                userLike.setLikeUserId(userId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(true);
                 mongoTemplate.save(userLike);

    }

    /*
    取消喜欢
     */
    @Override
    public void notLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        Update update = Update.update("isLike", false)
                .set("updated", System.currentTimeMillis());
        mongoTemplate.updateFirst(query, update, UserLike.class);
    }

    /*
    统计互相喜欢
     */
    @Override
    public Map counts(Long userId, Map count) {
        //查 互相喜欢 总数
       Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        List<UserLike> userLike = mongoTemplate.find(query, UserLike.class);
        if (userLike == null) {
            return null;
        }
        //喜欢用户也喜欢当前 互相喜欢
        List<UserLike> otherLike = new ArrayList<>();

        for (UserLike like : userLike) {
            Query query1 = Query.query(Criteria.where("userId").is(like.getLikeUserId())
                    .and("isLike").is(true));
            UserLike one = mongoTemplate.findOne(query1, UserLike.class);
            if (one!=null) {
                otherLike.add(one);
            }
        }
        //互相喜欢总数
        int otherLikeCount = otherLike.size();


        //查 喜欢 总数
        Query likeQuery = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        List<UserLike> likeList = mongoTemplate.find(likeQuery, UserLike.class);
        if (likeList==null){
            return null;
        }
        //喜欢总数
        int likeCount = likeList.size();


        //查粉丝总数
        Query fansQuery = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true));
        List<UserLike> fansList = mongoTemplate.find(fansQuery, UserLike.class);
        if (fansList == null){
            return null;
        }
        //粉丝总数
        int fansCount = fansList.size();

        //放入map
        count.put("eachLoveCount",Integer.valueOf(otherLikeCount));
        count.put("loveCount",Integer.valueOf(likeCount));
        count.put("fanCount",Integer.valueOf(fansCount));

        return count;

    }


}



